class Gams():
        distances = {}
	nodeid = 1
        def __init__(self,infilename,outfilename=None):
                self.infilename = infilename
                self.outfilename = outfilename

        def getDistances(self, level):
                lines = open(self.infilename).readlines()
                nodesTo = []
                if lines[0]:
                        arr = lines[0].split(" ")
			self.nodeid += 1
			arr[0] = str(self.nodeid)
                        for e in arr:
                                if e != "":
                                        #nodesTo.append(str(int(level)+1)+"_"+str(int(e.strip())))
                                        nodesTo.append(str(self.nodeid))
					self.nodeid += 1
                print nodesTo
                for i in range( 1 , len(lines)):
                        arr = (lines[i].replace(";", "")).split(" ")
                        print arr[0].strip()
			if arr[0].strip() not in self.distances.keys():
				self.distances[arr[0].strip()] = {}
                        e = 0
                        for j in range(1, len(arr)):
                                if arr[j].strip() != "" and e<len(nodesTo):
                                        print nodesTo[e], arr[j]
                                        self.distances[arr[0].strip()][nodesTo[e]] = arr[j].strip()
                                        e+=1
                        #print self.distances
                return

        def getGraph(self):
                fp = open('ip_4.txt', 'w')
                for i in self.distances.keys():
                        if len(self.distances[i].keys()) > 0:
                                for j in self.distances[i].keys():
                                        fp.write(i + " " + j + " "+ self.distances[i][j]+ "\n")
                fp.close()

g = Gams("distances")
g.getDistances('0')
g.infilename ='distance2'
g.getDistances('1')               
#print g.distances
g.getGraph()
               
